//
//  RegisterVC.swift
//  LeGou
//
//  Created by lxm on 2022/7/23.
//

import UIKit
import RxRelay

class RegisterVC: ZMViewController, UITextFieldDelegate {

    //MARK:--------------------属性--------------------
//    let cityCode : BehaviorRelay<Int?> = BehaviorRelay(value: nil)
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var psdText: UITextField!
    
    @IBOutlet weak var ensurePsdText: UITextField!
    
    @IBOutlet weak var addreesText: UITextField!
    
    @IBOutlet weak var loginButton: UIButton!
    
    var adcode = 0
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    //MARK:--------------------UI--------------------
    func setUI() {
        title = "密码设置"
        addreesText.delegate = self
        setLeftBtn(imgStr: "")
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }
    
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        let psd = psdText.rx.text.orEmpty.map({$0.zm.match(CONFIG_PSD_REGULAR)})
        let psdEnsure = ensurePsdText.rx.text.orEmpty.map({$0.zm.match(CONFIG_PSD_REGULAR)})
        let address = addreesText.rx.text.orEmpty.map({$0.count > 0})
        
        Observable.combineLatest([psd, psdEnsure, address]).map { oks ->Bool in
            for ok in oks {
                if !ok {return false}
            }
            return true
        }.subscribe(onNext:{ [unowned self] resp in
            self.loginButton.isEnabled = resp
            self.loginButton.backgroundColor = resp ? Style.mainColor : Style.disableColor
        }).disposed(by: disposeBag)
    }

    
    override func leftBtnClick(sender: UIButton) {
        
    }
    
    @IBAction func onNext(_ sender: Any) {
        if psdText.text != ensurePsdText.text {
            ZMToast.showToast(title: "两次输入密码不一致")
            return
        }
        if adcode == 0 {
            ZMToast.showToast(title: "请选择注册区域")
            return
        }
        ZMToast.showActivity()
        LoginAPI.setPsdAndCity(["password" : psdText.text!, "cityId" : adcode]).rxRequestNullAble(type: Any.self).subscribe(onNext:{ [unowned self] resp in
            ZMAppShareData.share.requestUserInfo { ok in
                ZMToast.hiddenActivity()
                if LoginService.shared.checkIsInfoOk() {
                    ZMJumpHelper.destroy(FromVC: self, toFirstClass: LoginVC.classForCoder(), isContainSelf: true, isContainFirst: true)
                    self.navigationController?.popViewController(animated: true)
                }
            }
        }).disposed(by: disposeBag)
    }
    

    //MARK:--------------------delegate--------------------
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if textField == addreesText {
            view.endEditing(true)
            ZMProvinceCityView.show { [unowned self] (p, c, a) in
                addreesText.text = "\(p.name) \(c.name) \(a?.name ?? "")"
                self.adcode = a?.cityId ?? c.cityId
//                cityCode.accept(a?.code)
                addreesText.sendActions(for: .editingChanged)
            }
            return false
        }
        return true
    }

}
